package com.ac.service.impl;

import com.ac.common.ServerResponse;
import com.ac.dao.UserMapper;
import com.ac.pojo.User;
import com.ac.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("iUserService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询所有用户,不需要传入参数
     * @return 如果有,返回用户list,如果数据库为空,返回查询失败
     */
    @Override
    public ServerResponse<List<User>> selectAll() {
        List<User> list = new ArrayList<User>();
        list = userMapper.selectAll();
        if (list.size() == 0){
            return ServerResponse.createByErrorMessage("数据库中没有数据");
        }else {
            return ServerResponse.createBySuccess("查询成功",list);
        }
    }

    /**
     * 查询单个用户
     * @param username
     * @return
     */
    @Override
    public ServerResponse<User> selectSingleUser(String username) {
        User user = null;
        user = userMapper.selectSingleUser(username);
        if (user == null){
            return ServerResponse.createByErrorMessage("查询失败,没有此用户");
        }
        return ServerResponse.createBySuccess("查询成功",user);
    }

    /**
     * 添加用户,首先判断用户名是否存在,如是则返回用户名已存在,如没有,则继续添加
     * @param user controller封装好的待添加user对象
     * @return
     */
    @Override
    public ServerResponse addUser(User user) {
        int result = 0;
        String username = user.getUsername();
        result = userMapper.checkUserName(username);
        if (result > 0){
            return ServerResponse.createByErrorMessage("用户名已存在");
        }
        result = 0;
        result = userMapper.insertSelective(user);
        if (result == 0){
            return ServerResponse.createByErrorMessage("添加失败");
        }
        return ServerResponse.createBySuccessMessage("添加成功");
    }

    /**
     * 删除用户,判断信息是否正确再删除
     * @param username
     * @param password
     * @param email
     * @return
     */
    @Override
    public ServerResponse deleteUser(String username, String password, String email) {
        int result = 0;
        result = userMapper.checkUserInfo(username,password,email);
        if (result == 0){
            return ServerResponse.createByErrorMessage("信息错误,删除失败");
        }
        result = 0;
        result = userMapper.deleteByUsername(username);
        if (result == 0 ){
            return ServerResponse.createByErrorMessage("删除失败");
        }
        return ServerResponse.createBySuccessMessage("删除成功");

    }

    /**
     * 根据用户名修改信息
     * @return
     */
    @Override
    public ServerResponse<User> updateUserInfo(User user) {
        int result = 0;
        String username = user.getUsername();
        result = userMapper.checkUserName(username);
        if (result == 0){
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        result = 0;
        result = userMapper.updateByUsername(user);
        if (result == 0){
            return ServerResponse.createByErrorMessage("用户更新失败");
        }
        user = userMapper.selectSingleUser(username);
        return ServerResponse.createBySuccess("更新成功",user);
    }

    /**
     * pageHelper分页方法
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ServerResponse getUserList(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<User> list = new ArrayList<User>();
        list = userMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }
}
